<?php
/**

 * [Laike System] Copyright (c) 2017-2020 laiketui.com

 * Laike is not a free software, it under the license terms, visited http://www.laiketui.com/ for more details.

 */
require_once(MO_LIB_DIR . '/DBAction.class.php');

class modifyAction extends Action {

    public function getDefaultView() {

        $db = DBAction::getInstance();
        $request = $this->getContext()->getRequest();
        // 接收分类id
        $cat_id = intval($request->getParameter("cat_id")); // 文章分类id


        // 根据分类id,查询分类表
        $sql = "select * from lkt_article_class where cat_id = '$cat_id'";
        $r = $db->select($sql);
        if($r){
            $cat_name = $r[0]->cat_name; // 文章分类名称
            $sort = $r[0]->sort; // 排序
        }
        // var_dump( $sort);

            // $articleclass = $this->articleclass($cat_id);//分类名称
            // $request->setAttribute("articleclass", $articleclass);//分类名称



        $request->setAttribute("cat_id",$cat_id);
        $request->setAttribute("cat_name",$cat_name);
        $request->setAttribute('sort', $sort);


        return View :: INPUT;
    }

    public function articleclass($article_cat_id1)
        {//文章分类
            $db = DBAction::getInstance();
            $sql01 = "select cat_id ,cat_name from lkt_article_class ";
            $r01 = $db->select($sql01);
            $articleclass = '';
            // $articleclass_num = 0;
            if ($r01) {
                if ($article_cat_id1) {
                    foreach ($r01 as $k01 => $v01) {
                        if ($v01->cat_id == $article_cat_id1) {
                            $articleclass .= '<option selected value="' . $v01->cat_id . '">' . $v01->cat_name . '</option>';
                        } else {
                            $articleclass .= '<option  value="' . $v01->cat_id . '">' . $v01->cat_name . '</option>';
                        }
                    }
                } else {
                    foreach ($r01 as $k2 => $v2) {
                        $articleclass .= '<option  value="' . $v2->cat_id . '">' . $v2->cat_name . '</option>';

                    }
                }
            }
            return $articleclass;

        }
    public function execute(){
      $db = DBAction::getInstance();
      $request = $this->getContext()->getRequest();
      $admin_id = $this->getContext()->getStorage()->read('admin_id');

        // $cat_id = intval($request->getParameter('cat_id')); // 文章分类id
        $cat_id = intval($request->getParameter("cat_id")); // 文章分类id
                // $r = $db->update($sql);

        // var_dump($cat_id);
        // die();

        $cat_name = addslashes(trim($request->getParameter('cat_name'))); // 文章分类名称

        $sort = addslashes(trim($request->getParameter('sort'))); // 排序



        //更新分类列表
        $sql = "update lkt_article_class " .
        "set cat_name = '$cat_name',sort = '$sort'"
        ." where cat_id = '$cat_id'";
        $r = $db->update($sql);

        // var_dump($sql);
        // die();

        if($r == -1) {
            $db->admin_record($admin_id,' 修改文章分类id为 '.$cat_id.' 失败',2);

            echo "<script type='text/javascript'>" .
            "alert('未知原因，修改文章分类失败！');" .
            "location.href='index.php?module=article_class';</script>";
            return $this->getDefaultView();
        } else {
            $db->admin_record($admin_id,' 修改文章分类id为 '.$cat_id.' 的信息',2);

            header("Content-type:text/html;charset=utf-8");
            echo "<script type='text/javascript'>" .
            "alert('修改文章分类成功！');" .
            "location.href='index.php?module=article_class';</script>";
        }
        return;
    }

    public function getRequestMethods(){
      return Request :: POST;
  }

}

?>